package com.vipheyue.bigbusiness.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.vipheyue.bigbusiness.bean.FavoritesTable;

import java.util.List;

/**
 * Created by heyue on 2015/10/28.
 */
public class MyDb {

    private static DbUtils db=null;

    public static List<FavoritesTable> getObjectTable(Context context) {
        if (db == null) {
            db = DbUtils.create(context);
        }
        List<FavoritesTable> FavoritesTables = null;//查询数据
        try {
            FavoritesTables = db.findAll(Selector.from(FavoritesTable.class));
        } catch (DbException e) {
            e.printStackTrace();
        }
        return FavoritesTables;
    }

    public static Boolean saveOrUpdate(Context context, Object entity) {
        if (db == null) {
            db = DbUtils.create(context);
        }
        try {
            db.saveOrUpdate(entity);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }
    public static SharedPreferences getSP(Context context) {
        SharedPreferences  sp = context.getSharedPreferences("dairycowplanConfig", Context.MODE_PRIVATE);
        return sp;
    }
    public  static void putSpBoolean(String info ,Boolean flag,Context context) {
        SharedPreferences  sp=  getSP(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(info, flag);
        editor.commit();
    }
}
